package edu.bit.water.controller;

import com.alibaba.fastjson.JSONObject;
import edu.bit.water.entity.ComponentReturn;
import edu.bit.water.entity.GalleryComponent;
import edu.bit.water.service.GalleryComponentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程图控件控制层
 *
 * @author ZZL
 */

@Controller
@RequestMapping("/component")
public class GalleryComponentController {
    @Autowired
    GalleryComponentService galleryComponentService;

    /**
     * 增加/修改流程图控件相关数据的方法
     *
     * @param jsonObject 控件实体类
     * @return 返回操作信息
     */
    @ResponseBody
    @RequestMapping("/insert")
    public Map<String, String> insert(@RequestBody JSONObject jsonObject) {
        GalleryComponent galleryComponent = new GalleryComponent();
        galleryComponent.setLabel(jsonObject.getString("label"));
        galleryComponent.setType(jsonObject.getString("type"));
        String s = jsonObject.getString("data");
        galleryComponent.setData(s.getBytes(StandardCharsets.UTF_8));
        galleryComponent.setCompanyName(jsonObject.getString("companyName"));
        Map<String, String> map = new HashMap<>();
        if ("add".equals(jsonObject.getString("controltype"))){
            if (galleryComponentService.insert(galleryComponent) >=1){
                map.put("status", "success");
            }else {
                map.put("status", "failed");
            }
        }else {
            galleryComponent.setId(jsonObject.getIntValue("id"));
            if (galleryComponentService.updateById(galleryComponent) >=1){
                map.put("status", "success");
            }else {
                map.put("status", "failed");
            }
        }
        return map;

    }

    /**
     * 删除控件的方法
     *
     * @param idList 将要被删除id的list集合
     * @return 返回操作信息
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map<String, String> delete(@RequestBody List<Integer> idList) {
        int n = galleryComponentService.deleteBatchIds(idList);
        Map<String, String> map = new HashMap<>();
        if (n >= 1)
            map.put("status", "success");
        else
            map.put("status", "failed");
        return map;
    }

    /**
     * 查询所有控件的方法
     *
     * @return 成功返回GalleryComponent集合, 失败返回NULL
     */
    @ResponseBody
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public Map<String, Object> select(String cname) {
        List<GalleryComponent> ls = galleryComponentService.selectList(cname);
        List<ComponentReturn> cr = new ArrayList<>();
        for (GalleryComponent i :
                ls) {
            String s = new String(i.getData(), StandardCharsets.UTF_8);
            ComponentReturn j = new ComponentReturn(i.getId(),i.getLabel(),i.getType(),s);
            cr.add(j);
        }
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("rows", cr);
        return modelMap;
    }
}
